package com.ng.framework.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class NGPropertyPlaceHolderConfigurer extends PropertyPlaceholderConfigurer {

	private static Logger LOG = Logger.getLogger(NGPropertyPlaceHolderConfigurer.class);

	public static final String PROPERTIES_STRATEGY = "PROPERTIES_STRATEGY";
	public static final String AVOID_DEFAULT_PROPERTIES = "AVOID_DEFAULT_PROPERTIES";
	public static final String OVERRIDE_DEFAULT_PROPERTIES = "OVERRIDE_DEFAULT_PROPERTIES";

	@Override
	protected void loadProperties(Properties props) throws IOException {

		String strategy = System.getProperty(PROPERTIES_STRATEGY);

		if (StringUtils.isEmpty(strategy)) {
			LOG.info("Using default configuration");
			super.loadProperties(props);

		} else {
			
			super.loadProperties(props);

			if (strategy.equals(OVERRIDE_DEFAULT_PROPERTIES)) {
				LOG.info("Overriding default properties : " + strategy);
				props.setProperty("database.url", System.getProperty("database.url"));
				props.setProperty("database.username", System.getProperty("database.username"));
				props.setProperty("database.password", System.getProperty("database.password"));
				
				

			}

		}

	}
}
